package org.apache.portals.tutorials;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class TestPortlet extends GenericPortlet{

	@Override
	protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException,IOException{
		PrintWriter out = response.getWriter();
		out.print("Edit mode");
	}

	@Override
	protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException,IOException{
		PrintWriter out = response.getWriter();
		out.print("Help mode");
	}

	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException{
		String userName = request.getParameter("userName");
		String email = request.getParameter("email");
		String actionResult = "false";

		System.out.println("processAction");
		System.out.println("userName: " + userName);
		System.out.println("email: " + email);

		if(stringIsEmpty(userName) || stringIsEmpty(email)){
			String errorMsg = "user name and email are mandatory";
			response.setRenderParameter("errorMsg", errorMsg);
		}else{
			actionResult = "true";
		}

		// set result in request
		if(!stringIsEmpty(userName)){
			response.setRenderParameter("userName", userName);
		}

		if(!stringIsEmpty(email)){
			response.setRenderParameter("email", email);
		}
		response.setRenderParameter("actionResult", actionResult);
	}

	@Override
	protected void doView(RenderRequest request, RenderResponse response) throws PortletException,IOException{
		String actionResult = request.getParameter("actionResult");

		PortletURL registerActionUrl = response.createActionURL();
		request.setAttribute("registerActionUrl", registerActionUrl.toString());

		PortletURL formUrl = response.createRenderURL();
		request.setAttribute("formUrl", formUrl.toString());

		request.setAttribute("userName", request.getParameter("userName"));
		request.setAttribute("email", request.getParameter("email"));
		request.setAttribute("errorMsg", request.getParameter("errorMsg"));

		String viewFile = null;

		if(actionResult == null || "false".equals(actionResult)){
			viewFile = "/WEB-INF/jsp/register-form.jsp";
		}else{
			viewFile = "/WEB-INF/jsp/success.jsp";
		}

		getPortletContext().getRequestDispatcher(viewFile).forward(request, response);
	}

	private boolean stringIsEmpty(String s){
		return s == null || s.length() == 0;
	}

}
